package com.lhx.product.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lhx.common.core.domain.R;
import com.lhx.product.domain.Product;
import com.lhx.product.domain.ProductResult;
import com.lhx.product.domain.dto.ProductDTO;
import com.lhx.product.domain.dto.ProductParam;
import com.lhx.product.domain.dto.ProductQueryParam;
import com.lhx.product.domain.po.ParentPO;
import com.lhx.product.domain.po.ProductPO;
import com.lhx.product.result.PageResult;
import com.lhx.product.domain.vo.ProductAttributeVO;
import com.lhx.product.domain.vo.ProductVO;

import com.lhx.product.result.Result;
import com.lhx.product.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author Lenovo
 */
@RestController
@RequestMapping("/product")
@Api(tags = "商品模块")

public class ProductController {
    @Autowired
    private ProductService productService;


    /**
     * 创建商品
     * @param productParam
     * @return
     */
    @ApiOperation("创建商品")
    @PostMapping(value = "/create")
    public Result create(@RequestBody ProductParam productParam) {
        int count = productService.create(productParam);
        return count>0?Result.success(productParam):Result.error();
    }


    /**
     * 更新商品
     * @param productParam
     * @return
     */
    @ApiOperation("更新商品")
    @PostMapping("/update/{id}")
    public Result update(@PathVariable Long id,@RequestBody ProductParam productParam){
        int flag=productService.update(id,productParam);
        return flag>0?Result.success(productParam):Result.error();
    }

    /**
     * 根据商品id获取商品编辑信息
     * @param id
     * @return
     */
    @ApiOperation("根据商品id获取商品编辑信息")
    @RequestMapping(value = "/updateInfo/{id}", method = RequestMethod.GET)
    @ResponseBody
    public R<ProductResult> getUpdateInfo(@PathVariable Long id) {
        ProductResult productResult = productService.getUpdateInfo(id);
        return R.ok(productResult);
    }

    /**
     * 商品列表
     * @param productQueryParam
     * @return
     */
    @ApiOperation("查询商品")
    @PostMapping("/list")
    public Result<PageResult<ProductVO>> list(@RequestBody ProductQueryParam productQueryParam){
        PageHelper.startPage(productQueryParam.getPageNum(),productQueryParam.getPageSize());
        List<ProductVO> list=productService.list(productQueryParam);
        PageInfo<ProductVO> objectPageInfo = new PageInfo<>(list);
        return Result.success(PageResult.toPageResult(objectPageInfo.getTotal(),list));
    }
//    @ApiOperation("查询商品")
//    @RequestMapping(value = "/list", method = RequestMethod.GET)
//    @ResponseBody
//    public R<PageResult<ProductVO>> getList(ProductQueryParam productQueryParam,
//                                                     @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
//                                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
//        PageHelper.startPage(pageNum, pageSize);
//        List<ProductVO> productList = productService.list(productQueryParam);
//        PageInfo<ProductVO> objectPageInfo = new PageInfo<>(productList);
//        return R.ok(PageResult.toPageResult(objectPageInfo.getTotal(),productList));
//    }


    @PostMapping("/productAddALL")
    @ApiOperation(value = "商品添加ALL")
    public R productAddParam(@RequestBody ProductParam productParam){
        return productService.productAddParam(productParam);
    }

    /**
     * 商品列表
     * @param productVO
     * @return
     */
    @PostMapping("/productAll")
    @ApiOperation(value = "商品列表")
    public R productAll(@RequestBody ProductVO productVO){
        List<ProductPO> productAll = productService.findProductAll(productVO);
        return R.ok(productAll);
    }

    /**
     * 商品删除
     * @param id
     * @return
     */
    @DeleteMapping("/productDele")
    @ApiOperation(value = "商品删除")
    public R productDele(@NotBlank(message = "不能为空")@RequestParam("id") String id){
        return productService.productDele(id);
    }

    /**
     * 商品修改
     * @param ProductPO
     * @return
     */
    @PostMapping("/productUpdate")
    @ApiOperation(value = "商品修改")
    public R productUpdate(@NotBlank(message = "不能有空值")@RequestBody ProductPO ProductPO){
        return productService.productUpdate(ProductPO);
    }

    /**
     * 商品添加
     * @param productDTO
     * @return
     */
    @PostMapping("/productAdds")
    @ApiOperation(value = "商品添加")
    public R productAdd(@NotBlank(message = "不能有空值")@RequestBody ProductDTO productDTO){
        productService.productAdds(productDTO);
        return R.ok();
    }

    /**
     * 分类列表
     * @param parentPo
     * @return
     */
    @PostMapping("/parentAll")
    @ApiOperation(value = "分类列表")
    public R productAll(@RequestBody ParentPO parentPo){
        List<ParentPO> parentPos = productService.parentAll(parentPo);
        return R.ok(parentPos);
    }

    /**
     * 分类删除
     * @param id
     * @return
     */
    @DeleteMapping("/parentDele")
    @ApiOperation(value = "分类删除")
    public R parentDele(@NotBlank(message = "不能为空")@RequestParam("id") String id){
        return productService.parentDele(id);
    }

    /**
     * 分类修改
     * @param parentPo
     * @return
     */
    @PostMapping("/parentUpdate")
    @ApiOperation(value = "分类修改")
    public R parentUpdate(@NotBlank(message = "不能有空值")@RequestBody ParentPO parentPo){
        return productService.parentUpdate(parentPo);
    }

    /**
     * 分类添加
     * @param parentPo
     * @return
     */
    @PostMapping("/parentAdd")
    @ApiOperation(value = "分类添加")
    public R parentAdd(@NotBlank(message = "不能有空值")@RequestBody ParentPO parentPo){
        return productService.parentAdd(parentPo);
    }



}
